﻿namespace UFIDA.U9.Cust.SH.SHFastShipBP
{
	using System;
	using System.Collections.Generic;
	using System.Text; 
	using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;
    using UFIDA.U9.SM.Ship;
    using UFIDA.U9.Base;
    using System.Data;
    using UFSoft.UBF.Util.DataAccess;
    using UFSoft.UBF.Sys.Database;
    using UFIDA.U9.CC.CreditObjectPolicy;
    using UFIDA.U9.CC.CreditBP.Proxy;
    using UFIDA.U9.CC.CreditSV.Proxy;
    using UFIDA.U9.CC.CreditBP;
    using UFIDA.U9.Cust.SH.SHFastShipBP.SHFastShipBP;

	/// <summary>
	/// ShipCreditCheck partial 
	/// </summary>	
	public partial class ShipCreditCheck 
	{	
		internal BaseStrategy Select()
		{
			return new ShipCreditCheckImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class ShipCreditCheckImpementStrategy : BaseStrategy
	{
		public ShipCreditCheckImpementStrategy() { }

		public override object Do(object obj)
		{						
			ShipCreditCheck bpObj = (ShipCreditCheck)obj;
			
			//get business operation context is as follows
			//IContext context = ContextManager.Context	
			
			//auto generating code end,underside is user custom code
			//and if you Implement replace this Exception Code...
			//throw new NotImplementedException();

            Ship ship = Ship.Finder.FindByID(bpObj.ShipID);
            if (ship == null) throw new Exception("出货单查找失败！");

            #region//Ant 3.26 审核后检验信用额度
            string shipDocNo = ship.DocNo;
            long OrgID = Context.LoginOrg.ID;
            //long OrgID = 1001106046291924;      //双虎组织id   1001106046291924
            long CustomerID = ship.OrderBy.CustomerKey.ID;
            string CustomerCode = ship.OrderBy.Customer.Code;

            //检验信用额度
            string result = CheckShipCredit(1, shipDocNo, CustomerID, OrgID, CustomerCode);// UFIDA.U9.Base.Context.LoginOrg.ID
            if (string.IsNullOrEmpty(result) != true)
            {
                //if (PrintType == 1)
                //executeDefault = false;
                //else
                //    executeDefault = true;

                //SetErrorMsg(this.ShipDocPart.Model.Views["Ship"].FocusedRecord, null, result);

                throw new Exception(result);
            }
            #endregion

            return null;
		}

        #region//校验信用额度
        private string CheckShipCredit(int docType, string docNo, long customerID, long org, string CustomerCode)
        {
            string result = "";
            if (customerID > 0)
            {
                try
                {
                    DataSet dskz = new DataSet();
                    //双虎组织
                    long OrgID = 1001106046291924;
                    string sqlkz = " SELECT b.Value FROM Base_Profile_Trl A ,BASE_ProfileValue b WHERE a.ID = b.Profile and  A.Name LIKE '%启用信用控制%' and b.Organization = " + OrgID;
                    int icountkz = DataAccessor.RunSQL(DatabaseManager.GetCurrentConnection(), sqlkz, null, out dskz);
                    if (icountkz > 0)
                    {
                        if (dskz.Tables[0].Rows[0]["Value"].ToString() == "True")
                        {
                            //客户获取信用等级信息BP 
                            //((PageBase)((UFSoft.UBF.UI.FormProcess.BaseWebForm)this.ShipDocPart).Page).ChangeCurrentOrg(OrgID.ToString());
                            GetObjectCreditLevelProxy obj = new GetObjectCreditLevelProxy();
                            obj.CustomerCode = CustomerCode;
                            ObjectCreditLevelData dto = obj.Do();
                            //((PageBase)((UFSoft.UBF.UI.FormProcess.BaseWebForm)this.ShipDocPart).Page).ChangeCurrentOrg(ShipDocPart.Model.Views[0].FocusedRecord["Org"].ToString());


                            //根据信用等级获得信用占用情况SV

                            ObjectCreditLimitStatSVProxy SVobj = new ObjectCreditLimitStatSVProxy();
                            SVobj.ObjectCreditLevel = dto.ID;

                            //SVobj.TargetOrgCode = ShipDocPart.Model.Views[0].FocusedRecord["Org_Code"].ToString();
                            //SVobj.TargetOrgName = ShipDocPart.Model.Views[0].FocusedRecord["Org_Name"].ToString();
                            //SVobj.ObjectCreditLevel_SKey = dto.CreditLevel_SKey;
                            //CreditLimitRateStatDTOData SVdto = SVobj.Do(Convert.ToInt64(ShipDocPart.Model.Views[0].FocusedRecord["AccountOrg"].ToString()));
                            //CreditLimitRateStatDTOData SVdto = SVobj.Do(OrgID);
                            CreditLimitRateStatDTOData SVdto = SVobj.Do();

                            //set @YYED = @ARBalance * @ARBalanceRate  + @BadDebts * @BadDebtsRate 
                            //+@Shipmented * @ShipmentedRate + @Shipmenting *@ShipmenttingRate
                            //+ @Ordered * @OrderedRat - @UndoReturnMoney * @WithdrawalRate 
                            //-@PreGathering *@PregatheringRate  


                            //
                            decimal KHYFS = PubTool.ToDecimal(SVdto.ARBalance.ToString()) + PubTool.ToDecimal(SVdto.BadDebts.ToString())
                                + PubTool.ToDecimal(SVdto.Shipmented.ToString()) + PubTool.ToDecimal(SVdto.Shipmenting.ToString()) 
                                + PubTool.ToDecimal(SVdto.Ordered.ToString())
                                - PubTool.ToDecimal(SVdto.UndoReturnMoney.ToString()) - PubTool.ToDecimal(SVdto.PreGathering.ToString());

                            DataParamList para = new DataParamList();
                            para.Add(UFSoft.UBF.Util.DataAccess.DataParamFactory.CreateInput("DocType", docType, DbType.Int16));

                            para.Add(UFSoft.UBF.Util.DataAccess.DataParamFactory.CreateInput("DocNo", docNo, DbType.String));

                            para.Add(UFSoft.UBF.Util.DataAccess.DataParamFactory.CreateInput("CustomerID", customerID, DbType.Int64));

                            para.Add(UFSoft.UBF.Util.DataAccess.DataParamFactory.CreateInput("Org", org, DbType.Int64));

                            para.Add(UFSoft.UBF.Util.DataAccess.DataParamFactory.CreateInput("KHYFS", KHYFS, DbType.Decimal));

                            para.Add(UFSoft.UBF.Util.DataAccess.DataParamFactory.CreateOutput("Remark", DbType.String));

                            DataAccessor.RunSP(DatabaseManager.GetCurrentConnection(), "Cust_CheckGetCredit_Proc", para);//调用存储过程检查数据
                            result = para["Remark"].Value.ToString();
                            //result = "SVdto.ARBalance:" + SVdto.ARBalance.ToString() + " "
                            //        + "SVdto.BadDebts:" + SVdto.BadDebts.ToString() + " "
                            //        + "SVdto.Shipmented:" + SVdto.Shipmented.ToString() + " "
                            //        + "SVdto.Shipmenting:" + SVdto.Shipmenting.ToString() + " "
                            //        + "SVdto.Ordered:" + SVdto.Ordered.ToString() + " "
                            //        + "SVdto.UndoReturnMoney:" + SVdto.UndoReturnMoney.ToString() + " "
                            //        + "SVdto.PreGathering:" + SVdto.PreGathering.ToString() + " ";
                        }
                    }

                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }

            }

            return result;
        }
        #endregion

	}

	#endregion
	
	
}